package com.yomob.recommend.controller;

import com.yomob.recommend.entity.AdEntity;
import com.yomob.recommend.service.AdService;
import com.yomob.recommend.util.CustomErrorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class RestApiController {

    public static final Logger logger = LoggerFactory.getLogger(RestApiController.class);
    @Value("{model.save.path}")
    private String modelPath;
    @Value("{user.ad.cache.path}")
    private AdService adService;
    @Autowired
    public RestApiController(AdService adService){
        this.adService = adService;
    }
    @RequestMapping(value = "/deviceId/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> getAd(@PathVariable("id") String id) {
        logger.info("Recommend Ads by deviceId {}.", id);
        AdEntity ad = adService.recommendByDeviceId(id);
        if (ad == null) {
            logger.error("Ad recommend by deviceId {} not found.", id);
            return new ResponseEntity(new CustomErrorType("Ad recommend by deviceId  " + id
                    + " not found"), HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(ad, HttpStatus.OK);
    }

}
